package com.deviche.taoke.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.deviche.taoke.GoodsPaser;
import com.deviche.taoke.R;
import com.deviche.taoke.base.BaseFragment;
import com.deviche.taoke.bean.Goods;
import com.deviche.taoke.common.Constant;
import com.deviche.taoke.ui.activity.GoodsDetailActivity;
import com.deviche.taoke.ui.adapter.CommonAdapter;

import java.util.ArrayList;

/**
 * Created by h on 2018/4/20.
 */

public class YxzbFragment extends BaseFragment {
    //获取数据完毕刷新ui
    private static final int UPDATE = 2000;
    //严选直播页面
    private RecyclerView mRecyclerView;
    private ArrayList<Goods> datas;
    private CommonAdapter adapter;
    public static final String TAG = YxzbFragment.class.getSimpleName();

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UPDATE){
                //adapter.notifyDataSetChanged();
                //创建适配器
                adapter = new CommonAdapter(R.layout.item_rv, datas);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent();
                        intent.setClass(mContext,GoodsDetailActivity.class);
                        startActivity(intent);
                        Toast.makeText(mContext, "onItemClick" + position, Toast.LENGTH_SHORT).show();
                    }
                });
                //给RecyclerView设置适配器
                mRecyclerView.setAdapter(adapter);
            }

        }
    };
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_yxzb;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mRecyclerView = view.findViewById(R.id.rv_yxzb);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));

        datas = new ArrayList<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                datas = GoodsPaser.parseFrom(Constant.HOST);

                Message message = Message.obtain();
                message.what = UPDATE;
                handler.sendMessage(message);
            }
        }).start();

    }

    @Override
    protected void initData() {

    }
}
